namespace CIS.Project
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ProjectInfoPage : Page
    {
        protected Image iBtnProject;
        protected Button newUserBtn;
        protected UserPrincipal Principal = null;
        protected DataGrid projectListDg;

        private void FillPage()
        {
            this.projectListDgDataBind();
        }

        private void InitializeComponent()
        {
            this.newUserBtn.Click += new EventHandler(this.newUserBtn_Click);
            this.projectListDg.ItemCommand += new DataGridCommandEventHandler(this.projectListDg_ItemCommand);
            this.projectListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.projectListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.projectListDg.PageSize = 14;
        }

        private void newUserBtn_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("~/Project/ProjectInfoEditPage.aspx?CompanyId=" + base.Request.QueryString["CompanyId"].ToString(), true);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void projectListDg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string cmd = e.CommandName;
            string strProjectId = e.CommandArgument.ToString();
            Guid projectId = new Guid();
            try
            {
                projectId = new Guid(strProjectId);
            }
            catch (Exception)
            {
                return;
            }
            switch (cmd)
            {
                case "Edit":
                    base.Response.Redirect("~/Project/ProjectInfoEditPage.aspx?IsModify=True&ProjectId=" + projectId.ToString() + "&CompanyId=" + base.Request.QueryString["CompanyId"].ToString(), true);
                    break;

                case "delete":
                    ManageProject.RemoveProjectViaId(projectId);
                    if ((this.projectListDg.Items.Count == 1) && (this.projectListDg.CurrentPageIndex > 0))
                    {
                        this.projectListDg.CurrentPageIndex--;
                    }
                    this.projectListDgDataBind();
                    break;
            }
        }

        private void projectListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.projectListDg.CurrentPageIndex = e.NewPageIndex;
            this.projectListDgDataBind();
        }

        private void projectListDgDataBind()
        {
            try
            {
                Guid companyId = new Guid(base.Request.QueryString["CompanyId"]);
                this.projectListDg.DataSource = ManageProject.GetProjectInfo(companyId);
                this.projectListDg.DataBind();
            }
            catch (Exception)
            {
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                this.InitPage();
                this.FillPage();
                this.newUserBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeB == 1)
            {
                this.InitPage();
                this.FillPage();
            }
            else if (this.Principal.OwnRole.TypeB == 2)
            {
                this.newUserBtn.Visible = false;
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.newUserBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoPage", "newUserBtn", language, path);
            this.projectListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoPage", "Name", language, path);
            this.projectListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoPage", "Amount", language, path);
            this.projectListDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoPage", "SDate", language, path);
            this.projectListDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoPage", "EDate", language, path);
            this.projectListDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoPage", "Edit", language, path);
            this.projectListDg.Columns[5].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoPage", "Delete", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iBtnProject.ImageUrl = "../images/title_ProjectInfo.jpg";
                    break;

                case "en":
                    this.iBtnProject.ImageUrl = "../images/title_ProjectInfoEn.jpg";
                    break;
            }
        }
    }
}

